package com.xing;

import org.eclipse.jetty.server.Slf4jRequestLog;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.jetty.JettyServerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * Hello world!
 */
//@EnableAutoConfiguration
//@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableScheduling
public class Application {

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(Application.class);
        springApplication.run(args);
    }

    /**
     * 嵌入jetty，自定义配置
     *
     * @return
     */
    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        JettyEmbeddedServletContainerFactory factory =
                new JettyEmbeddedServletContainerFactory();

        factory.setPort(9000);
//        factory.setSessionTimeout(10, TimeUnit.MINUTES);
        //factory.addErrorPages(new ErrorPage(HttpStatlus.NOT_FOUND, "/notfound.html"));
        factory.addServerCustomizers(jettyServerCustomizer());
        return factory;
    }

    /**
     * 自定义配置ls
     */
    @Bean
    public JettyServerCustomizer jettyServerCustomizer() {
        return server -> {
            // Tweak the connection config used by Jetty to handle incoming HTTP
            // connections
            final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class);
            threadPool.setMaxThreads(100);
            threadPool.setMinThreads(20);
            //jetty配置accesslog
            server.setRequestLog(new Slf4jRequestLog());
        };
    }


    /**
     * 嵌入tomcat
     */
//    @Bean
//    public EmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() throws IOException {
//        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
//        tomcat.addAdditionalTomcatConnectors(httpConnector());
//        return tomcat;
//    }
//
//    public Connector httpConnector() throws IOException {
//        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
//        Http11NioProtocol http11NioProtocol = (Http11NioProtocol) connector.getProtocolHandler();
//        connector.setPort(8080);
//        //设置最大线程数
//        http11NioProtocol.setMaxThreads(100);
//        //设置初始线程数  最小空闲线程数
//        http11NioProtocol.setMinSpareThreads(20);
//        //设置超时
//        http11NioProtocol.setConnectionTimeout(5000);
//        return connector;
//    }
}
